package com.wyj.utils;

import cn.hutool.core.date.DateTime;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @author ：wyj
 * @description：TODO
 * @date ：2023/12/28 11:13 PM
 */
public class DateUtil {


    public static final String TIME_FORMAT_COMMON = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_FORMAT_FLAG_ONE = "yyyyMMdd";
    public static final String TIME_FORMAT_FLAG_TWO = "yyyy:MM:dd";

    /**
     * @return
     * @throws
     * @description 获取到当前的时间 yyyy-mm-dd HH:mm:ss
     * @author wyj
     * @date 2023/12/28 11:13 PM
     * @Param: null
     */
    public static String getNowTime() {
        SimpleDateFormat format = new SimpleDateFormat(TIME_FORMAT_COMMON);
        return format.format(new Date());
    }


//    public static LocalDateTime getNow() {
//       return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DateUtil.TIME_FORMAT_COMMON));
//    }

    /**
     * @author wyj
     * @date 2024/1/1 9:10 PM
     * @description 获取到3小时后的时间戳
     */
    public static long getTimeAfterMinutes(int minutes) {
        return System.currentTimeMillis() + minutes * 60 * 1000;
    }

    /**
     * @author wyj
     * @date 2024/1/3 9:08 PM
     * @description 获取特定时间的时间戳
     */
    public static long getMills(String timeFlag, String formatter) {
        DateTime dateTime = cn.hutool.core.date.DateUtil.parse(timeFlag, DateTimeFormatter.ofPattern(formatter));
        return dateTime.toCalendar().getTimeInMillis();
    }

    /**
     * @author wyj
     * @date 2024/1/3 9:24 PM
     * @description 获取到今天的年月日 yyyyMMdd
     */

    public static String getTimeFlag(String format) {
        LocalDateTime now = LocalDateTime.now();
        String flag = now.format(DateTimeFormatter.ofPattern(format));
        return flag;
    }

    public static long getLocalDateTimeMills(LocalDateTime beginTime) {
        String format = beginTime.format(DateTimeFormatter.ofPattern(DateUtil.TIME_FORMAT_COMMON));
        return DateUtil.getMills(format, DateUtil.TIME_FORMAT_COMMON);
    }

    /**
     * 年月日相差几天
     */
    public static long getDifferDay(String start,String end) {
        LocalDate startTime = LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyyMMdd"));
        LocalDate endTime = LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyyMMdd"));
        long between = ChronoUnit.DAYS.between(startTime, endTime);
        return between;
    }

    public static long getExpireMills(long expire) {
        long ms = expire - System.currentTimeMillis();
        if (ms < 0) {
            ms = 0;
        }
        return ms;
    }

    /**
     * @author wyj
     * @date 2024/1/21 6:03 PM
     * @description 获取到这个月的第几天
     */
    public static int getNowMonthDay() {
        LocalDateTime now = LocalDateTime.now();
        return now.getDayOfMonth();
    }

    public static void main(String[] args) {
        long mills = getMills("2023-01-01 12:21:12", DateUtil.TIME_FORMAT_COMMON);
        System.out.println(mills);
        String timeFlag = getTimeFlag(TIME_FORMAT_FLAG_TWO);
        System.out.println(timeFlag);
    }


}
